package lbframework.bean.calculadora;

import java.awt.event.KeyEvent;
import javax.swing.JPopupMenu;
import lbframework.bean.panel.LBPanel;
import lbframework.classe.Util;
import lbframework.classe.LBException;

public class LBCalculadora extends LBPanel {

    private JPopupMenu popUp = new JPopupMenu();
    
    public LBCalculadora() {
        initComponents();
    }

    public void abrirCalculadora() throws Exception {
        popUp.add(pnlPopUp);
        int x = txtValor.getX();
        int y = txtValor.getY() + txtValor.getHeight();

        popUp.setSize(pnlPopUp.getWidth(), pnlPopUp.getHeight());
        popUp.show(this, x, y);

        txtCampo.requestFocus();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlPopUp = new lbframework.bean.panel.LBPanel();
        LBPanel1 = new lbframework.bean.panel.LBPanel();
        btn7 = new lbframework.bean.button.LBButton();
        btnC = new lbframework.bean.button.LBButton();
        txtCampo = new lbframework.bean.textField.LBTextField();
        btn0 = new lbframework.bean.button.LBButton();
        btnPonto = new lbframework.bean.button.LBButton();
        btnAdicao = new lbframework.bean.button.LBButton();
        btnIgual = new lbframework.bean.button.LBButton();
        btn1 = new lbframework.bean.button.LBButton();
        btn2 = new lbframework.bean.button.LBButton();
        btn3 = new lbframework.bean.button.LBButton();
        btnSubtracao = new lbframework.bean.button.LBButton();
        btn4 = new lbframework.bean.button.LBButton();
        btn5 = new lbframework.bean.button.LBButton();
        btn6 = new lbframework.bean.button.LBButton();
        btnMultiplicacao = new lbframework.bean.button.LBButton();
        btn8 = new lbframework.bean.button.LBButton();
        btnDivisao = new lbframework.bean.button.LBButton();
        btn9 = new lbframework.bean.button.LBButton();
        txtValor = new lbframework.bean.textField.LBTextField();
        btnCalculadora = new lbframework.bean.button.LBButton();

        pnlPopUp.setBorder(null);

        LBPanel1.setBorder(null);

        btn7.setText("7");

        btnC.setText("C");

        btn0.setText("0");

        btnPonto.setText(".");

        btnAdicao.setText("+");

        btnIgual.setText("=");

        btn1.setText("1");

        btn2.setText("2");

        btn3.setText("3");

        btnSubtracao.setText("-");

        btn4.setText("4");

        btn5.setText("5");

        btn6.setText("6");

        btnMultiplicacao.setText("*");

        btn8.setText("8");

        btnDivisao.setText("/");

        btn9.setText("9");

        javax.swing.GroupLayout LBPanel1Layout = new javax.swing.GroupLayout(LBPanel1);
        LBPanel1.setLayout(LBPanel1Layout);
        LBPanel1Layout.setHorizontalGroup(
            LBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(txtCampo, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(btnC, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(LBPanel1Layout.createSequentialGroup()
                .addComponent(btn7, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btn8, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btn9, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnDivisao, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(LBPanel1Layout.createSequentialGroup()
                .addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btn5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btn6, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnMultiplicacao, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(LBPanel1Layout.createSequentialGroup()
                .addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnSubtracao, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(LBPanel1Layout.createSequentialGroup()
                .addComponent(btnPonto, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btn0, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnIgual, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnAdicao, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        LBPanel1Layout.setVerticalGroup(
            LBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(LBPanel1Layout.createSequentialGroup()
                .addComponent(txtCampo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(btnC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addGroup(LBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDivisao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(1, 1, 1)
                .addGroup(LBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnMultiplicacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(LBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSubtracao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(LBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnPonto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnIgual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdicao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout pnlPopUpLayout = new javax.swing.GroupLayout(pnlPopUp);
        pnlPopUp.setLayout(pnlPopUpLayout);
        pnlPopUpLayout.setHorizontalGroup(
            pnlPopUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(LBPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        pnlPopUpLayout.setVerticalGroup(
            pnlPopUpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(LBPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        setBorder(null);

        txtValor.setMascara("Decimal 2");
        txtValor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtValorKeyPressed(evt);
            }
        });

        btnCalculadora.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/bean/calculadora/calculadora.png"))); // NOI18N
        btnCalculadora.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCalculadoraActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(txtValor, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCalculadora, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(btnCalculadora, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCalculadoraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCalculadoraActionPerformed
        try {
            abrirCalculadora();
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }//GEN-LAST:event_btnCalculadoraActionPerformed

    private void txtValorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtValorKeyPressed
        try {
            if(evt.getKeyCode() == KeyEvent.VK_F9){
               abrirCalculadora();
            }
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }//GEN-LAST:event_txtValorKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.button.LBButton btn0;
    private lbframework.bean.button.LBButton btn1;
    private lbframework.bean.button.LBButton btn2;
    private lbframework.bean.button.LBButton btn3;
    private lbframework.bean.button.LBButton btn4;
    private lbframework.bean.button.LBButton btn5;
    private lbframework.bean.button.LBButton btn6;
    private lbframework.bean.button.LBButton btn7;
    private lbframework.bean.button.LBButton btn8;
    private lbframework.bean.button.LBButton btn9;
    private lbframework.bean.button.LBButton btnAdicao;
    private lbframework.bean.button.LBButton btnC;
    private lbframework.bean.button.LBButton btnCalculadora;
    private lbframework.bean.button.LBButton btnDivisao;
    private lbframework.bean.button.LBButton btnIgual;
    private lbframework.bean.button.LBButton btnMultiplicacao;
    private lbframework.bean.button.LBButton btnPonto;
    private lbframework.bean.button.LBButton btnSubtracao;
    private lbframework.bean.panel.LBPanel pnlPopUp;
    private lbframework.bean.textField.LBTextField txtCampo;
    private lbframework.bean.textField.LBTextField txtValor;
    private lbframework.bean.panel.LBPanel LBPanel1;
    // End of variables declaration//GEN-END:variables

}
